﻿using ArtMath.Attribute;
using ArtMath.Component.Line;
using ArtMath.Component.Point;
using ArtMath.Core.Data;
using ArtMath.Core.Helper;
using ArtMath.PointCollection;
using ArtMath.Resource;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ArtMath.Component.Polygon
{
    /// <summary>
    /// 等腰直角三角形
    /// </summary>
    [Serializable]
    public class IsoscelesRightTriangle : TriangleBase
    {
        public IsoscelesRightTriangle(SketchPad pad, string name, bool addToList = true) : base(pad, name, ToolTypeName.dyzjsjx, addToList)
        {
        }
        [MultilingualDisplayName("{0}1", "RightAngleWaist")]
        public override LineSegmentBase Line1
        {
            get
            {
                return base.Line1;
            }

            set
            {
                base.Line1 = value;
            }
        }
        [MultilingualDisplayName("{0}2", "RightAngleWaist")]
        public override LineSegmentBase Line2
        {
            get
            {
                return base.Line2;
            }

            set
            {
                base.Line2 = value;
            }
        }
        [MultilingualDisplayName("HypotenuseHemline")]
        public override LineSegmentBase Line3
        {
            get
            {
                return base.Line3;
            }

            set
            {
                base.Line3 = value;
            }
        }
        public override void ComponentBuilding(Base component)
        {
            PointD tmp;
            if (OrderIndex == 2)
            {
                if (component == Line1)
                {
                    tmp = Line1.Point1.ToPointD().RotateAt((Line3.Point1.ToPointD() + Line3.Point2.ToPointD()) / 2, 
                        Core.Helper.Angle.RightAngle * Pad.Sign(Line1.Point1.Pxy, (Line3.Point1.Pxy + Line3.Point2.Pxy) / 2, Line1.SpareCoordinate));
                    Line1.SpareCoordinate_ = Pad.XYToPxy(tmp);
                    calcCenters(Line3.Point1.ToPointD(), Line1.Point1.ToPointD(), tmp);
                }
                else if (component == Line3)
                {
                    Circumference = Line3.Length * (1 + Math.Sqrt(2));
                    Area = Line3.Length * Line3.Length / 4;
                }
            }
            else
            {
                if (component == Line1)
                {
                    Circumference = Line1.Length * (2 + Math.Sqrt(2));
                    Area = Line1.Length * Line1.Length / 2;
                }
                else
                {
                    if (OrderIndex == 0)
                    {
                        if (component == Line2)
                        {
                            tmp = Line1.Point1.ToPointD().RotateAt(Line2.Point1.ToPointD(), Core.Helper.Angle.RightAngle * Pad.Sign(Line1.Point1.Pxy, Line2.Point1.Pxy, Line2.SpareCoordinate));
                            Line2.SpareCoordinate_ = Pad.XYToPxy(tmp);
                            calcCenters(Line1.Point1.ToPointD(), Line2.Point1.ToPointD(), tmp);
                        }
                    }
                    else
                    {
                        if (component == Line3)
                        {
                            tmp = Line3.Point1.ToPointD().RotateAt(Line1.Point1.ToPointD(), Core.Helper.Angle.RightAngle * Pad.Sign(Line3.Point1.Pxy, Line1.Point1.Pxy, Line3.SpareCoordinate));
                            Line3.SpareCoordinate_ = Pad.XYToPxy(tmp);
                            calcCenters(Line1.Point1.ToPointD(), Line3.Point1.ToPointD(), tmp);
                        }
                    }
                }
            }
            base.ComponentBuilding(component);
        }
        public override PointCollectionBase[] GetInPointMovingRange(PointBase movePoint, Base sender = null)
        {
            switch (CountMovablePoints())
            {
                case 1:
                    if (movePoint == Line1.Point1)
                    {
                        if (OrderIndex == 1)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line3.Point1.NewXY.RotateAt(Line3.NewMidPoint, Core.Helper.Angle.RightAngle), Line3.Point1.NewXY.RotateAt(Line3.NewMidPoint, -Core.Helper.Angle.RightAngle)) };
                        }
                        else
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line3.Point1.NewXY.RotateAt(Line2.Point1.NewXY, Core.Helper.Angle.RightAngle), Line3.Point1.NewXY.RotateAt(Line2.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                    }
                    else if (movePoint == Line2.Point1)
                    {
                        if (OrderIndex == 1)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line3.Point1.NewXY.RotateAt(Line1.Point1.NewXY, Core.Helper.Angle.RightAngle), Line3.Point1.NewXY.RotateAt(Line1.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                        else
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line3.Point1.NewXY.RotateAt(Line3.NewMidPoint, Core.Helper.Angle.RightAngle), Line3.Point1.NewXY.RotateAt(Line3.NewMidPoint, -Core.Helper.Angle.RightAngle)) };
                        }
                    }
                    else if (movePoint == Line3.Point1)
                    {
                        if (OrderIndex == 1)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line2.Point1.NewXY.RotateAt(Line1.Point1.NewXY, Core.Helper.Angle.RightAngle), Line2.Point1.NewXY.RotateAt(Line1.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                        else
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line1.Point1.NewXY.RotateAt(Line2.Point1.NewXY, Core.Helper.Angle.RightAngle), Line1.Point1.NewXY.RotateAt(Line2.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                    }
                    break;
            }
            return base.GetInPointMovingRange(movePoint, sender);
        }
        public override void CreateComponents()
        {
            switch (OrderIndex)
            {
                case 0:
                    if (Line1 == null)
                    {
                        Line1 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("RightAngleWaist")}1", false) { SpareCoordinate_ = SpareCoordinate_, };
                        Line1.CreateComponents($"{Resources.Translate("RightAngleWaist")}1{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("HypotenuseHemline")}{Resources.Join}{Resources.Translate("EndPoint")}2");
                    }
                    else if (Line2 == null)
                    {
                        Line1.CreateComponents($"{Resources.Translate("RightAngleWaist")}2{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("RightAngleWaist")}1{Resources.Join}{Resources.Translate("EndPoint")}2");
                        Line2 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("RightAngleWaist")}2", false) { AllowFix = false };
                        Line2.CreateComponents(Line1.Point2);
                    }
                    else
                    {
                        Line2.CreateComponents($"{Resources.Translate("HypotenuseHemline")}{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("RightAngleWaist")}2{Resources.Join}{Resources.Translate("EndPoint")}2", new PointCollectionBase[] { new DiscontinuityPoint(Line1.Point1.ToPointD().RotateAt(Line2.Point1.ToPointD(), Core.Helper.Angle.RightAngle), Line1.Point1.ToPointD().RotateAt(Line2.Point1.ToPointD(), -Core.Helper.Angle.RightAngle)) });
                        Line3 = new TwoPointsLineSegment(Pad, Resources.Translate("HypotenuseHemline"), false);
                        Line3.CreateComponents(Line2.Point2, Line1.Point1);
                        base.CreateComponents();
                    }
                    break;
                case 1:
                    if (Line1 == null)
                    {
                        Line1 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("RightAngleWaist")}1", false) { SpareCoordinate_ = SpareCoordinate_, };
                        Line1.CreateComponents($"{Resources.Translate("RightAngleWaist")}1{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("RightAngleWaist")}2{Resources.Join}{Resources.Translate("EndPoint")}2");
                    }
                    else if (Line3 == null)
                    {
                        Line1.CreateComponents($"{Resources.Translate("HypotenuseHemline")}{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("RightAngleWaist")}1{Resources.Join}{Resources.Translate("EndPoint")}2");
                        Line3 = new TwoPointsLineSegment(Pad, Resources.Translate("HypotenuseHemline"), false) { AllowFix = false };
                        Line3.CreateComponents(Line1.Point2);
                    }
                    else
                    {
                        Line3.CreateComponents($"{Resources.Translate("RightAngleWaist")}2{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("HypotenuseHemline")}{Resources.Join}{Resources.Translate("EndPoint")}2", new PointCollectionBase[] { new DiscontinuityPoint(Line3.Point1.ToPointD().RotateAt(Line1.Point1.ToPointD(), Core.Helper.Angle.RightAngle), Line3.Point1.ToPointD().RotateAt(Line1.Point1.ToPointD(), -Core.Helper.Angle.RightAngle)) });
                        Line2 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("RightAngleWaist")}2", false);
                        Line2.CreateComponents(Line3.Point2, Line1.Point1);
                        base.CreateComponents();
                    }
                    break;
                case 2:
                    if (Line3 == null)
                    {
                        Line3 = new TwoPointsLineSegment(Pad, Resources.Translate("HypotenuseHemline"), false) { SpareCoordinate_ = SpareCoordinate_, };
                        Line3.CreateComponents($"{Resources.Translate("HypotenuseHemline")}{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("RightAngleWaist")}2{Resources.Join}{Resources.Translate("EndPoint")}2");
                    }
                    else if (Line1 == null)
                    {
                        Line3.CreateComponents($"{Resources.Translate("RightAngleWaist")}1{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("HypotenuseHemline")}{Resources.Join}{Resources.Translate("EndPoint")}2");
                        Line1 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("RightAngleWaist")}1", false) { AllowFix = false };
                        Line1.CreateComponents(Line3.Point2);
                    }
                    else
                    {
                        Line1.CreateComponents($"{Resources.Translate("RightAngleWaist")}2{Resources.Join}{Resources.Translate("EndPoint")}1/{Resources.Translate("RightAngleWaist")}1{Resources.Join}{Resources.Translate("EndPoint")}2", new PointCollectionBase[] { new DiscontinuityPoint(Line3.Point1.ToPointD().RotateAt(Line3.MidPoint, Core.Helper.Angle.RightAngle), Line3.Point1.ToPointD().RotateAt(Line3.MidPoint, -Core.Helper.Angle.RightAngle)) });
                        Line2 = new TwoPointsLineSegment(Pad, $"{Resources.Translate("RightAngleWaist")}2", false);
                        Line2.CreateComponents(Line1.Point2, Line3.Point1);
                        base.CreateComponents();
                    }
                    break;
            }
        }
    }
}
